<script setup lang="ts">
  import { ref, computed, watch } from 'vue';
  import { useBooleanStore } from '@/store';
  import { searchMsTaskAo } from '@/api/sorting-matching';

  const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'id',
      slotName: 'id',
      align: 'center',
      ellipsis: true,
      tooltip: true,
      width: 80,
    },
    {
      title: '机型',
      dataIndex: 'productCode',
      ellipsis: true,
      tooltip: true,
      width: 100,
    },
    {
      title: '批次',
      dataIndex: 'batchNo',
      ellipsis: true,
      tooltip: true,
      width: 80,
    },
    {
      title: '起始架次',
      dataIndex: 'startPlaneNo',
      ellipsis: true,
      tooltip: true,
      width: 180,
    },

    {
      title: '结束架次',
      dataIndex: 'endPlaneNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      ellipsis: true,
      tooltip: true,
      width: 180,
    },
    {
      title: '工作包编号',
      dataIndex: 'packageCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      ellipsis: true,
      tooltip: true,
      width: 200,
    },

    {
      title: '工作包名称',
      dataIndex: 'packageName',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      ellipsis: true,
      tooltip: true,
      width: 200,
    },
    {
      title: 'AO编号',
      dataIndex: 'aoCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      ellipsis: true,
      tooltip: true,
      width: 180,
    },

    {
      title: 'AO名称',
      dataIndex: 'aoName',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      ellipsis: true,
      tooltip: true,
      width: 180,
    },
    {
      title: '要求时间',
      dataIndex: 'requiredTime',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      ellipsis: true,
      tooltip: true,
      width: 180,
    },

    {
      title: '备注',
      dataIndex: 'receiveRemark',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      ellipsis: true,
      tooltip: true,
      width: 200,
    },
  ];
  const booleanStore = useBooleanStore();
  const hStyle = ref({
    'background-color': booleanStore.color,
    'border-radius': booleanStore.borderRadius,
  });

  // 创建一个响应式的本地副本
  const localTableData = ref<any[]>([]);
  // 数据库一共有多少条
  const total = ref<number>(0);
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(30);

  const val: any = ref('');
  async function getLocalList(value: any) {
    const info = ref({
      msTaskPackageId: computed(() => value),
      pageParameter: computed(() => ({
        rows: pageSize.value,
        page: pageNum.value,
      })),
    });
    const data: any = await searchMsTaskAo(info.value);
    localTableData.value = data.responseBody?.result;
    total.value = data.responseBody.pageParameter.totalCount;
  }

  //
  watch(
    () => booleanStore.list,
    () => {
      if (booleanStore.num7 === 12) {
        val.value = booleanStore.list[0] || '';
        getLocalList(val.value?.msTaskPackageId);
      }
    },
    { immediate: true }
  );
  // 显示AO物料配套清单
  function showData(record: any) {
    booleanStore.setListTwo(record, 1);
  }
  function change(e: any) {
    pageNum.value = e;
  }
  function changeSize(e: any) {
    pageSize.value = e;
  }
  // // 监听页面变化
  // watch([pageNum, pageSize], () => {
  //   booleanStore.setPage(pageNum.value, pageSize.value);
  // });
</script>

<template>
  <a-card :header-style="hStyle" :bordered="false">
    <template #title>
      <div class="font">AO分拣任务列表</div>
    </template>

    <a-table
      :stripe="true"
      class="top-table"
      :pagination="false"
      :columns="columns"
      :sticky-header="true"
      :data="localTableData"
      :scroll="{ y: 400 }"
      style="height: 250px"
      @row-click="showData"
    >
      <template #id="{ record }">
        {{ localTableData.indexOf(record) + 1 }}
      </template>
    </a-table>
    <div class="page-position">
      <a-pagination
        :total="total"
        :current="pageNum"
        :page-size="pageSize"
        show-total
        show-jumper
        show-page-size
        :page-size-options="[30, 50, 100, 500]"
        @change="change"
        @update:page-size="changeSize"
      />
    </div>
  </a-card>
</template>

<style scoped>
  .top-table {
    white-space: nowrap;
    z-index: 1;
  }
  .page-position {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .font {
    font-size: 20px;
    color: black;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
      Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue',
      sans-serif;
  }
</style>
